﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace YhdSdk
{
    /// <summary>
    /// 符合Yhd习惯的纯字符串字典结构。
    /// </summary>
    public class YhdDictionary : Dictionary<string, string>
    {
        private const string DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

        public YhdDictionary() { }

        public YhdDictionary(IDictionary<string, string> dictionary)
            : base(dictionary)
        { }

        /// <summary>
        /// 添加一个新的键值对。空键或者空值的键值对将会被忽略。
        /// </summary>
        /// <param name="key">键名称</param>
        /// <param name="value">键对应的值，目前支持：string, int, long, double, bool, DateTime类型</param>
        public void Add(string key, object value)
        {
            string strValue;

            if (value == null)
            {
                strValue = null;
            }
            else if (value is string)
            {
                strValue = (string)value;
            }
            else if (value is DateTime)
            {
                strValue = ((DateTime)value).ToString(DATE_TIME_FORMAT, System.Globalization.DateTimeFormatInfo.InvariantInfo);
            }
            else if (value is Boolean)
            {
                strValue = ((Boolean)value).ToString().ToLower();
            }
            else if (value is Nullable<DateTime>)
            {
                Nullable<DateTime> dateTime = value as Nullable<DateTime>;
                strValue = dateTime.HasValue ? dateTime.Value.ToString(DATE_TIME_FORMAT, System.Globalization.DateTimeFormatInfo.InvariantInfo) : null;
            }
            else if (value is Nullable<Int16>)
            {
                Nullable<Int16> v = value as Nullable<Int16>;
                strValue = v.HasValue ? v.Value.ToString() : null;
            }
            else if (value is Nullable<Int32>)
            {
                Nullable<Int32> v = value as Nullable<Int32>;
                strValue = v.HasValue ? v.Value.ToString() : null;
            }
            else if (value is Nullable<Int64>)
            {
                Nullable<Int64> v = value as Nullable<Int64>;
                strValue = v.HasValue ? v.Value.ToString() : null;
            }
            else if (value is Nullable<Single>)
            {
                Nullable<Single> v = value as Nullable<Single>;
                strValue = v.HasValue ? v.Value.ToString() : null;
            }
            else if (value is Nullable<Decimal>)
            {
                Nullable<Decimal> v = value as Nullable<Decimal>;
                strValue = v.HasValue ? v.Value.ToString() : null;
            }
            else if (value is Nullable<Double>)
            {
                Nullable<Double> v = value as Nullable<Double>;
                strValue = v.HasValue ? v.Value.ToString() : null;
            }
            else if (value is Nullable<Boolean>)
            {
                Nullable<Boolean> v = value as Nullable<Boolean>;
                strValue = v.HasValue ? v.Value.ToString().ToLower() : null;
            }
            else if (value is List<String>)
            {
                List<String> v = value as List<String>;
                strValue = String.Join(",", v.ToArray());
            }
            else if (value is IEnumerable)
            {
                IEnumerable v = value as IEnumerable;
                strValue = Join(",", v);
            }
            else
            {
                strValue = value.ToString();
            }

            this.Add(key, strValue);
        }

        public new void Add(string key, string value)
        {
            //if (!string.IsNullOrEmpty(key))
            if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
            {
                base.Add(key, value);
            }
        }

        public string Join(string separator, IEnumerable values)
        {
            List<Object> objs = new List<object>();
            foreach (var item in values)
            {
                objs.Add(item);
            }

            return Join(separator, objs.ToArray());
        }

        public string Join(string separator, params object[] values)
        {
            if (values == null) throw new ArgumentNullException("values");
            if (values.Length == 0 || values[0] == null) return String.Empty;
            if (separator == null) separator = ",";
            StringBuilder builder = new StringBuilder();
            string str = values[0].ToString();
            if (str != null) builder.Append(str);
            for (int i = 1; i < values.Length; i++)
            {
                builder.Append(separator);
                if (values[i] != null)
                {
                    str = values[i].ToString();
                    if (str != null) builder.Append(str);
                }
            }
            return builder.ToString();
        }
    }
}
